package com.qinyunkj.searchchatserver.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qinyunkj.searchchatserver.model.common.Result;
import com.qinyunkj.searchchatserver.model.entity.Article;
import com.qinyunkj.searchchatserver.model.request.ArticleQuery;
import com.qinyunkj.searchchatserver.model.vo.ArticleStatistics;
import com.qinyunkj.searchchatserver.service.ArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "文章管理")
@RestController
@RequestMapping("/api/articles")
@RequiredArgsConstructor
@Validated
public class ArticleController {

    private final ArticleService articleService;

    @Operation(summary = "分页查询文章")
    @GetMapping
    public Page<Article> list(
        @RequestParam(defaultValue = "1") Integer pageNum,
        @RequestParam(defaultValue = "10") Integer pageSize,
        @RequestParam(required = false) String keyword,
        @RequestParam(required = false) String category,
        @RequestParam(required = false) List<String> tags
    ) {
        return articleService.getArticleList(pageNum, pageSize, keyword, category, tags);
    }

    @Operation(summary = "根据分类获取文章")
    @GetMapping("/category/{category}")
    public Result<Page<Article>> getArticlesByCategory(
            @PathVariable String category,
            @RequestParam(defaultValue = "1") @Min(1) Integer pageNum,
            @RequestParam(defaultValue = "10") @Min(1) Integer pageSize) {
        return Result.success(articleService.getArticlesByCategory(category, pageNum, pageSize));
    }

    @Operation(summary = "获取文章详情")
    @GetMapping("/{id}")
    public Result<Article> getArticle(@PathVariable Long id) {
        return Result.success(articleService.getById(id));
    }

    @Operation(summary = "获取所有分类")
    @GetMapping("/categories")
    public Result<List<String>> getAllCategories() {
        List<String> categories = articleService.getAllCategories();
        return Result.success(categories);
    }

    @Operation(summary = "手动触发爬虫")
    @PostMapping("/crawl")
    public Result<String> triggerCrawl(@RequestParam("url") String url) {
        articleService.crawl(url);
        return Result.success("爬虫任务已触发");
    }

    @Operation(summary = "获取热门文章")
    @GetMapping("/hot")
    public Result<List<Article>> getHotArticles(
            @Parameter(description = "获取条数")
            @RequestParam(defaultValue = "10") Integer limit) {
        return Result.success(articleService.getHotArticles(limit));
    }

    @Operation(summary = "根据标签获取文章")
    @GetMapping("/tag/{tag}")
    public Result<Page<Article>> getArticlesByTag(
            @Parameter(description = "标签名称")
            @PathVariable String tag,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(articleService.getArticlesByTag(tag, pageNum, pageSize));
    }

    @Operation(summary = "获取所有标签")
    @GetMapping("/tags")
    public Result<List<String>> getAllTags() {
        return Result.success(articleService.getAllTags());
    }

    @Operation(summary = "获取文章统计信息")
    @GetMapping("/statistics")
    public Result<ArticleStatistics> getStatistics() {
        return Result.success(articleService.getStatistics());
    }

    @Operation(summary = "创建文章")
    @PostMapping
    public Result<Article> createArticle(@RequestBody Article article) {
        articleService.save(article);
        return Result.success(article);
    }

    @Operation(summary = "更新文章")
    @PutMapping("/{id}")
    public Result<Article> updateArticle(@PathVariable Long id, @RequestBody Article article) {
        article.setId(id);
        articleService.updateById(article);
        return Result.success(article);
    }

    @Operation(summary = "删除文章")
    @DeleteMapping("/{id}")
    public Result<Void> deleteArticle(@PathVariable Long id) {
        articleService.removeById(id);
        return Result.success();
    }
}
